<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable>
          <el-option v-for="dict in order_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" style="width: 308px">
        <el-date-picker clearable v-model="paramDate" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" type="index" width="55px" />
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="商品名称" align="center" prop="skuName" />
      <el-table-column label="订单金额" align="center" prop="amount" />
      <el-table-column label="设备编号" align="center" prop="innerCode" />
      <el-table-column label="订单状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="order_status" :value="scope.row.status" />
        </template>
      </el-table-column>

      <el-table-column label="订单日期" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" @click="handleDetail(scope.row)"
            v-hasPermi="['manage:order:query']">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 订单详情对话框 -->
    <el-dialog title="订单详情" v-model="open" width="600px" append-to-body>
      <el-row>
        <el-col :span="13">
          订单编号：{{ form.orderNo }}
        </el-col>
        <el-col :span="11">
          商品名称：{{ form.skuName }}
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="13">
          订单金额：{{ form.amount }}
        </el-col>
        <el-col :span="11">
          
          <div v-for="(item, index) in order_status" :key="index" ><span v-if="item.value==form.status">订单状态：{{ item.label }}</span></div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="13">
          设备编号：{{ form.innerCode }}
        </el-col>
        <el-col :span="11">
          设备地址：{{ form.addr }}
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="13">
          创建时间：{{ form.createTime }}
        </el-col>
        <el-col :span="11">
          完成时间：{{ form.updateTime }}
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="13">
          支付方式：{{ form.payType==='wxpay'?'微信支付':'支付宝支付' }}
        </el-col>
        <el-col :span="11">
          交易单号：{{ form.thirdNo }}
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          操作：
          <el-button v-if="form.status==0"> 关闭订单 </el-button>
          <el-button v-else> 退款 </el-button>
        </el-col>
      </el-row>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Order">
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/manage/order";

const { proxy } = getCurrentInstance();
const { pay_status, pay_type, order_status } = proxy.useDict('pay_status', 'pay_type', 'order_status');

const orderList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

console.log(order_status)
const paramDate = ref([]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderNo: null,
    status: null,
    beginDate: null,
    endDate: null
  },
  rules: {
    orderNo: [
      { required: true, message: "订单编号不能为空", trigger: "blur" }
    ],
    amount: [
      { required: true, message: "支付金额不能为空", trigger: "blur" }
    ],
    price: [
      { required: true, message: "商品金额不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询订单管理列表 */
function getList() {
  loading.value = true;
  listOrder(queryParams.value).then(response => {
    orderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    orderNo: null,
    thirdNo: null,
    innerCode: null,
    channelCode: null,
    skuId: null,
    skuName: null,
    classId: null,
    status: null,
    amount: null,
    price: null,
    payType: null,
    payStatus: null,
    bill: null,
    addr: null,
    regionId: null,
    regionName: null,
    businessType: null,
    partnerId: null,
    openId: null,
    nodeId: null,
    nodeName: null,
    cancelDesc: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("orderRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  console.log(paramDate.value);
  if (paramDate.value.length > 0) {
    queryParams.value.beginDate = paramDate.value[0];
    queryParams.value.endDate = paramDate.value[1];
  } else {
    queryParams.value.beginDate = null;
    queryParams.value.endDate = null;
  }
  queryParams.value.pageNum = 1;
  getList();
}



/** 提交按钮 */
function submitForm() {
  proxy.$refs["orderRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}


getList();

// 查看订单详情
function handleDetail(row) {
  console.log(row);
  getOrder(row.id).then(response => {
    form.value = response.data;
    open.value = true;
  });
}
</script>

<style scoped>
.el-row {
  margin-bottom: 20px;
}

.el-row:last-child {
  margin-bottom: 0;
}

.el-col {
  border-radius: 4px;
}
</style>